<?php
session_start();
if (!isset($_SESSION['admin'])) { header('Location: index.php'); exit; }
$dataFile = '../data/projects.json';
$projects = file_exists($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
// 新建项目
if (isset($_POST['add'])) {
    $id = uniqid('p');
    $projects[$id] = [
        'id' => $id,
        'name' => $_POST['name'],
        'desc' => $_POST['desc'],
        'language' => $_POST['language'] ?? 'zh'
    ];
    file_put_contents($dataFile, json_encode($projects, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
    mkdir("../data/project_$id", 0777, true);
    header('Location: projects.php'); exit;
}
// 删除项目
if (isset($_GET['del'])) {
    $del = $_GET['del'];
    if (isset($projects[$del])) {
        unset($projects[$del]);
        file_put_contents($dataFile, json_encode($projects, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
        // 删除目录（递归）
        function deldir($dir) {
            if (!is_dir($dir)) return;
            $files = array_diff(scandir($dir), ['.','..']);
            foreach ($files as $file) {
                (is_dir("$dir/$file")) ? deldir("$dir/$file") : unlink("$dir/$file");
            }
            rmdir($dir);
        }
        deldir("../data/project_$del");
    }
    header('Location: projects.php'); exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>项目管理</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container py-4">
    <h4 class="mb-3">项目管理</h4>
    <form method="post" class="row g-2 align-items-end mb-4 bg-white p-3 rounded shadow-sm">
        <div class="col-md-3">
            <label class="form-label">项目名称</label>
            <input name="name" class="form-control" required>
        </div>
        <div class="col-md-4">
            <label class="form-label">项目描述</label>
            <input name="desc" class="form-control">
        </div>
        <div class="col-md-3">
            <label class="form-label">语言</label>
            <select name="language" class="form-control">
                <option value="zh">中文</option>
                <option value="en">English</option>
            </select>
        </div>
        <div class="col-md-2">
            <button type="submit" name="add" class="btn btn-primary">新建项目</button>
        </div>
    </form>
    <table class="table table-bordered table-hover align-middle bg-white">
        <thead><tr><th>名称</th><th>描述</th><th>语言</th><th>操作</th></tr></thead>
        <tbody>
        <?php foreach($projects as $p): ?>
        <tr>
            <td><?=htmlspecialchars($p['name'])?></td>
            <td><?=htmlspecialchars($p['desc'])?></td>
            <td><?=($p['language']??'zh') === 'zh' ? '中文' : 'English'?></td>
            <td>
                <a href="project_dashboard.php?pid=<?=$p['id']?>" class="btn btn-success btn-sm">进入管理</a>
                <a href="../download.php?pid=<?=$p['id']?>" class="btn btn-info btn-sm" target="_blank">预览下载页</a>
                <a href="#" onclick="return confirmDelete('<?=$p['id']?>')" class="btn btn-danger btn-sm">删除</a>
            </td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <a href="dashboard.php" class="btn btn-link btn-sm">返回后台首页</a>
</div>
<script>
function confirmDelete(projId) {
    var msg = '删除后该APP的下载二维码将失效，且所有数据不可恢复！\n\n如确定删除，请输入：确认删除';
    var input = prompt(msg);
    if (input === '确认删除') {
        location.href = '?del=' + encodeURIComponent(projId);
    } else if (input !== null) {
        alert('输入错误，未删除。');
    }
    return false;
}
</script>
</body>
</html> 